package cn.edu.scau.cmi.wangmaoren.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class AdapterOfObject implements Target
{
	@Override
	public List<Student> doTarget(List<Student> studentList)
	{
		Adaptee adaptee = new Adaptee();
		List<Integer> intlist = new ArrayList<Integer>();//整型数据队列
		List<Student> newStudentList = new ArrayList<Student>();//排序后新的学生队列
		HashMap<Integer, Student> studentMap = new HashMap();//学生哈希表

		/*
		 * 遍历学生表，分离出整型数据队列，并创建学生哈希表
		 */
		Iterator<Student> studentIter = studentList.iterator();
		while (studentIter.hasNext())
		{
			Student student = studentIter.next();
			intlist.add(student.getId());//将整型数据加入队列
			studentMap.put(student.getId(), student);//将学生对象加入哈希表
		}

		intlist = adaptee.op(intlist);//对整型数据队列进行排序

		/*
		 * 遍历整型数据队列，利用学生哈希表的key值获取对应学生姓名，并构造新的学生队列
		 */
		Iterator<Integer> integerIter = intlist.iterator();
		while (integerIter.hasNext())
		{
			Integer integer = integerIter.next();
			newStudentList.add(new Student(integer, studentMap.get(integer).getName()));
		}

		return newStudentList;
	}
}
